import streamlit as st
from langchain_openai import ChatOpenAI

# 设置页面标题和描述
st.title("Chat with AI")
st.write("Enter a message and get a response from the AI model.")


# 创建一个 ChatOpenAI 对象
llm = ChatOpenAI(model="gpt-3.5-turbo")

# 用户输入框
user_message = st.text_area("Your Message", height=150)

# 生成按钮
if st.button("Generate Response"):
    if user_message:
        try:
            # 调用 ChatOpenAI 实例
            response = llm([{"role": "user", "content": user_message}])
            # 显示模型的响应
            st.write("AI Response:")
            st.write(response.content)
        except Exception as e:
            st.error(f"An error occurred: {str(e)}")
    else:
        st.warning("Please enter a message to generate a response.")
